import fs from 'fs';

const getCatalogue = (path: string) => {
    if (fs.existsSync(path)) return fs.readdirSync(path);
    else return [];
};

const isFile = (filePath) => {
    try {
        return fs.statSync(filePath).isFile()
    } catch {
        return null;
    }
};

const isDir = (filePath) => {
    try {
        return fs.statSync(filePath).isDirectory()
    } catch {
        return null;
    }
};

const removeFile = (filePath) => {
    if (isFile(filePath)) {
        fs.unlinkSync(filePath);
    }
    
    else if (isDir(filePath)) {
        // 递归删除所有文件夹下文件
        const list = fs.readdirSync(filePath);
        list.forEach((docName) => {
            removeFile(`${filePath}/${docName}`);
        });
        fs.rmdirSync(filePath);
    }
}

export {
    getCatalogue,
    isFile,
    isDir,
    removeFile,
}